// pages/voiceRedPacket/tixian/tixian.js
import S_request from '../../../utils/requestService.js';
import CONFIG from '../../../config.js';
import * as utils from '../../../utils/util.js';
const app = getApp()
let redPackId = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money: 0,
    inputMoney: '',
    isShowShare:true,
    modalObj: {},
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    redPackId = options.redPackId;
    this.getUserMoney();
    this.showShareDia('分享给好友，带领好友一起发家致富');
  },
  getUserMoney: function() {
    S_request.redPack.myRedPackInfo((result) => {
      this.setData({
        money: result.user.redbalance
      })
    })
  },
  mobileInputEvent: function(e) {
    console.log("hfhvadhvfahInput=====" + e.detail.value)
    let tixianMoney = e.detail.value;
    this.setData({
      inputMoney: tixianMoney
    })
    let title = '';
    if (parseInt(tixianMoney) < 1) {
      title = '提现金额不能低于1元，别灰心~明天再来抢红包吧。'
      this.showToast(title)
    } else if (parseInt(tixianMoney) > parseInt(this.data.money)) {
      title = '提现金额超过账户余额'
      this.showToast(title)
    }
  },
  tixianAll: function() {
    this.setData({
      inputMoney: this.data.money
    })
    if (parseInt(this.data.money) < 1) {
      let title = '提现金额不能低于1元，别灰心~明天再来抢红包吧。'
      this.showToast(title)
    }
  },
  showToast: function(title) {
    wx.showToast({
      title: title,
      icon: 'none',
      duration: 2000
    })
  },
  tixian: function() {
    console.log("this.data.inputMoney===" + this.data.inputMoney)
    if (this.data.inputMoney == '') {
      wx.showModal({
        title: '提示',
        content: '请输入有效提现金额',
        showCancel: false
      })
    } else if (parseInt(this.data.inputMoney) < 1) {
      wx.showModal({
        title: '提示',
        content: '提现金额不能低于1元，别灰心~明天再来抢红包吧。',
        showCancel: false
      })
    } else if (parseInt(this.data.inputMoney) > parseInt(this.data.money)) {
      wx.showModal({
        title: '提现失败',
        content: '账户余额不足',
        showCancel: false
      })
    } else {
      S_request.redPack.tixian(this.data.inputMoney, (result) => {
        wx.showModal({
          title: '提现成功',
          content: '提现成功',
          showCancel: false,
          success:(res)=>{
            if (res.confirm) {
              this.showShareDia('我领钱啦！炫耀一下')
            } 
          }
        })
        this.getUserMoney();
        this.setData({
          inputMoney: ''
        })
      })
    }
  },
  showShareDia:function(title) {
    this.data.modalObj.title = title;
    this.setData({
      modalDealIdx: 1,
      modalObj: this.data.modalObj,
      isShowShare: true
    })
  },
  closeShareBtnAc: function (e) {
    this.setData({
      modalDealIdx: 0,
      isShowShare: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    wx.showShareMenu({
      withShareTicket: true,
      success: function () { }
    })
    let sharePath = CONFIG.SHARE.pathVoice + "&redPackId=" + redPackId;
    let obj = { 'pagepath': CONFIG.SHARE.pathShop, 'redPackId': redPackId,'sign':1 }
    S_request.index.recordShare("-5", obj);
    console.log("sharePath====" + sharePath)
    return {
      title: CONFIG.SHARE.titleVoice,
      path: sharePath,
      imageUrl: CONFIG.SHARE.imagVoiceUrl
    }
  }
})